// packageB/member/love/love_gift_bag/love_gift_bag.js
const app = getApp();
Page({
  data: {
    tabActive: '2',
    isReceive: false,
    showDialog: false,
    recordList: [],
    can_total: 0,
    stored_total: 0,
    gain_total: 0,
    past_total: 0,
    titleText: '',

    loading: false,
    finished: false,
    page: 1,

    redpack: {},
    audio: {},
    audioContext: null,
    imgPath: app.globalData.imgPath
  },

  onLoad: async function (options) {
    await this.getAudio();
    this.createAudio();
  },

  createAudio () {
    const audioContext = wx.createInnerAudioContext();
    audioContext.src = this.data.audio.voice_link;
    audioContext.autoplay = false;
    if (wx.setInnerAudioOption) {
      wx.setInnerAudioOption({
        obeyMuteSwitch: false,
      });
    }else {
      audioContext.obeyMuteSwitch = false;
    }
    this.setData({
      audioContext
    });
  },

  onShow: function () {
    this.getData();
  },

  switchTab (event) {
    this.setData({
      tabActive: event.detail.name
    });
    this.initData();
    this.getData();
  },

  getData () {
    if (this.data.finished) {
      return;
    }
    let urlStr = app.getNetAddresss('plugin.love-redpack.frontend.redpack.homePage');
    app._getNetWork({
      url: urlStr,
      data: { status: this.data.tabActive, page: this.data.page },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showModal({
            showCancel: false,
            content: respone.data.msg,
            success() {}
          });
          return;
        }
        let data = respone.data.data.list;
        let res = respone.data.data;
        let recordList = [];
        if (data.last_page === this.data.page || data.data.length < data.per_page) {
          this.setData({
            finished: true
          });
        }
        if (this.data.page > 1) {
          recordList = this.data.recordList.concat(data.data);
        } else {
          recordList = data.data;
        }
        wx.setNavigationBarTitle({
          title: res.love_name 
        });
        this.setData({
          page: ++this.data.page,
          can_total: res.can_total,
          stored_total: res.stored_total,
          gain_total: res.gain_total,
          past_total: res.past_total,
          recordList: recordList,
          isReceive: (res.can_total <= 1),
          titleText: res.love_name,
        });
      }
    });
  },

  getAudio () {
    let urlStr = app.getNetAddresss('plugin.love-redpack.frontend.voice.index');
    return new Promise((reslove, reject) => {
      app._getNetWork({
        url: urlStr,
        data: {},
        success: (respone) => {
          if (respone.data.result !== 1) {
            wx.showModal({
              showCancel: false,
              content: respone.data.msg,
              success() {}
            });
            reject(respone.data.data);
            return;
          }
          this.setData({
            audio: respone.data.data
          });
          reslove(respone.data.data);
        }
      });
    });
  },

  getRedpack () {
    let urlStr = app.getNetAddresss('plugin.love-redpack.frontend.redpack.getRedpack');
    return new Promise((reslove, reject)=>{
      app._getNetWork({
        url: urlStr,
        data: {},
        success: (respone) => {
          if (respone.data.result !== 1) {
            wx.showModal({
              showCancel: false,
              content: respone.data.msg,
              success() {}
            });
            reslove(respone.data.result);
            return;
          }
          this.setData({
            redpack: respone.data.data
          });
          reslove(respone.data.result);
        },
        fail (err) {
          reject(err);
        }
      });
    });
  },

  async showPopup() {
    if (this.data.can_total <= 0) return;
    let result = await this.getRedpack();
    if (result !== 1) return;
    if (this.data.audioContext && this.data.audio.is_open) {
      this.data.audioContext.seek(0);
      this.data.audioContext.play();
    }
    this.setData({
      showDialog: true,
      can_total: (--this.data.can_total)
    });
  },
  onClose () {
    this.data.audioContext.pause();
    this.initData();
    this.getData();
    this.setData({
      showDialog: false
    });
  },

  initData () {
    this.setData({
      showDialog: false,
      recordList: [],
      loading: false,
      finished: false,
      page: 1,
    });
  },

  onUnload: function () {
    this.data.audioContext.stop();
  }

});